$('#file').on('change', function () {
  const formData = new FormData();
  formData.append('image', this.files[0]);
  $.ajax({
    type: 'post',
    url: '/upload',
    data: formData,
    contentType: false,
    processData: false,
    success: function (response) {
      const image = response[0].image;
      $('#image').val(image);
    }
  })
})

// 添加轮播图
$('#slidesForm').on('submit', function () {
  const formData = $(this).serialize();
  $.ajax({
    type: 'post',
    url: '/slides',
    data: formData,
    success: function () {
      location.reload();
    }
  })
  return false;
})

// 获取列表数据
$.ajax({
  type: 'get',
  url: '/slides',
  success: function (response) {
    const html = template('slidesTpl', {
      data: response
    });
    $('#slidesBox').html(html);
  }
})

$('#slidesBox').on('click', '.delete', function () {
  if(confirm('您确认要删除该轮播图吗')){
    const id = $(this).attr('data-id');
    $.ajax({
      type: 'delete',
      url: '/slides/' + id,
      success: function () {
        location.reload();
      }
    })
  }
})